﻿$.fn.InitialEditTableCell = function (savebutton, activeClass) {
    var f2 = false;
    $($(this).find('td[editable="true"]')).click(function () {
        AddTextBox(this);
        f2 = false;
    });
    $($(this).find('td[editable="true"]')).keydown(function (event) {
        if (!f2) {
            var code = event.keyCode || event.which;
            switch (code) {
                case 37:
                    var pre = $(this).prev('td[editable="true"]');
                    if ($(pre).length > 0) {
                        AddTextBox(pre);
                        OutCell(this);
                    }
                    break;
                case 38:
                    var aboverow = $(this).parent().prev("tr");
                    if ($(aboverow).length > 0) {
                        var colindex = $(this).parent().children("td").index(this);
                        var editable = $($(aboverow).children()[colindex]).attr("editable");
                        if (typeof editable != "undefined" && editable.toLowerCase() == "true") {
                            AddTextBox($(aboverow).children()[colindex]);
                            OutCell(this);
                        }
                    }
                    break;
                case 39:
                    var next = $(this).next('td[editable="true"]');
                    if ($(next).length > 0) {
                        AddTextBox(next);
                        OutCell(this);
                    }
                    break;
                case 40:
                    var underrow = $(this).parent().next("tr");
                    if ($(underrow).length > 0) {
                        var colindex = $(this).parent().children("td").index(this);

                        var editable = $($(underrow).children()[colindex]).attr("editable");
                        if (typeof editable != "undefined" && editable.toLowerCase() == "true") {
                            AddTextBox($(underrow).children()[colindex]);
                            OutCell(this);
                        }
                    }
                    break;
                case 13:
                case 113:
                    return false;
            }
            f2 = false;
        }
        return true;
    });

    function AddTextBox(target) {
        if ($(target).children('input').length == 0) {
            var content = $.trim($(target).html());
            var curfontsize = $(target).css('font-size');
            var h = $(target).parent().height() - 3;
            if (content.toLowerCase() == "no check") {
                content = "";
            }
            var w = $(target).width() - 4;
            var faketxt = '<input type="text" style="width:' + w + 'px;height:' + h + 'px; border:0; font-size:' + curfontsize + '; background-color:transparent;" value="' + $.trim(content) + '"/>';
            $(target).html(faketxt);
            $(target).attr("value", $.trim(content));
        }

        $(target).addClass("focus");
        $(target).children("input").focus();
        $(target).children('input').focusout(function () {
            OutCell($(this).parent());
        });

        $(target).children('input').keydown(function (event) {
            var code = event.keyCode || event.which;
            switch (code) {
                case 113:
                    f2 = true;
                    return false;
                case 13:
                    if (GoDown($(this).parent()))
                        OutCell($(this).parent());
                    f2 = false;
                    return false;
                case 9:
                    if (GoNext($(this).parent()))
                        OutCell($(this).parent());
                    f2 = false;
                    return false;
            }
        });
    }

    function GoDown(cell) {
        var underrow = $(cell).parent().next("tr");
        if ($(underrow).length > 0) {
            var colindex = $(cell).parent().children("td").index(cell);
            var editable = $($(underrow).children()[colindex]).attr("editable");
            if (typeof editable != "undefined" && editable.toLowerCase() == "true") {
                AddTextBox($(underrow).children()[colindex]);
                f2 = false;
                return true;
            }
            else
                return false;
        }
    }

    function GoNext(cell) {
        var next = $(cell).next('td[editable="true"]');
        if ($(next).length > 0) {
            AddTextBox(next);
            f2 = false;
            return true;
        } else
            return false;
    }
    function OutCell(cell) {
        $(cell).removeClass("focus");
        if ($(cell).children("input").length > 0) {
            var editcontnt = $.trim($(cell).children("imput").val());
            var oldval = $(cell).attr("value");
            if (typeof oldval == "undefined" || oldval.toLowerCase() == "no check") {
                oldval = "";
            }
            if (oldval.toLowerCase() != editcontnt.toLowerCase()) {
                $(cell).parent().attr("changed", true);
                $(savebutton).addClass(activeClass);
            }
            if (editcontnt == "") {
                editcontnt = "no check";
            }
            $(cell).html(editcontnt);
            $(cell).textOverflow();
            $(cell).children("input").remove();
            f2 = false;
        }
    }
}